package com.lyn.utils;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * @author Lyn 2022/12/30
 * @version 1.0.0
 */
public class ObjectUtil {

    public static <T> T conversion(Object obj,T t) {
        Class<?> aClass = obj.getClass();
        Class<?> tClass = t.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field:fields){
            try {
                field.setAccessible(true);
                Object o = field.get(obj);
                if (!Objects.isNull(o)){
                    Field tField = tClass.getDeclaredField(field.getName());
                    tField.setAccessible(true);
                    if (field.getType()==tField.getType()){
                        tField.set(t,o);
                    }
                }
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return t;
    }
}
